<?php
/*
 * @Author: 李明
 * @Date: 2021-11-22 09:07:02
 * @LastEditTime: 2022-01-12 16:34:12
 * @LastEditors: 李明
 * @Description:
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/xcx/order/XcxOrderController.php
 * @山东壹零贰肆信息科技有限公司
 */

namespace App\Http\Controllers\xcx\order;

use App\Http\Controllers\common\LMReturn;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Utils;
use App\Http\Resources\OrderXcx;
use App\Models\Order;
use App\Models\Worker;

class XcxOrderController extends Controller
{

    public function workerOrderList(Request $request)
    {
        try {
            //去验证token值
            $tokenInfo = json_decode($this->verifyTokenLm(), true);
            //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
            if ($tokenInfo['code'] == 400) {
                return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
            }
            //接收前端传递过来的值
            $all = $request->all();
            $type = $all['type'] ?? 2;
            $worker_id = $tokenInfo['worker_id'];
            $where = $this->orderWhere($all, $worker_id);
            $pageSize = $all['size'] ?? 15;
        } catch (\Throwable $th) {
            return LMReturn::Error('查询条件存在问题，请联系管理员', 'OC023');
        }

        try {
            // 有权限看的部门
            $branch_id_list = Utils::getWorkerChildSonBranchs(Worker::find($worker_id));
            //查询这个部门的订单
            if (isset($all['manage_bm'])) { //有的话就查这个部门的，没有的话，就查个人的
                // 他想看的部门
                if (in_array($all['manage_bm'], $branch_id_list)) {
                    $branch_id_list = Utils::getChildSonBranchs($all['manage_bm']);
                } else {
                    $branch_id_list = [];
                }
            } else {
                if ($type == 1) {
                    $worker_id = $all['worker_id'] ?? $worker_id;
                    $where[] = ['worker_id', $worker_id];
                }
            }
            $order_list = Order::select('*')
                ->with('product')
                ->where($where)
                ->when($type==2, function ($query) use ($branch_id_list) {
                    return $query->whereIn('order.branch_id', $branch_id_list);
                })
                ->orderBy("order.update_time", "desc")
                ->paginate($pageSize);
        } catch (\Throwable $th) {
            return LMReturn::Error('查询存在问题，请联系管理员', 'OC029');
        }
        try {
            // 返回前端需要的数据
            $res_order_list = OrderXcx::collection($order_list);
            $order_list = json_decode(json_encode($order_list), true);
            $return_datas['current_page'] = $order_list['current_page'];
            $return_datas['from'] = $order_list['from'];
            $return_datas['last_page'] = $order_list['last_page'];
            $return_datas['per_page'] = $order_list['per_page'];
            $return_datas['to'] = $order_list['to'];
            $return_datas['total'] = $order_list['total'];
            $return_datas['data'] = $res_order_list;
            return LMReturn::Success($return_datas);
        } catch (\Throwable $th) {
            return LMReturn::Error('数据存在问题，请联系管理员', 'OC022');
        }
    }

    /**
     * @description: order 条件查询 条件构造
     * @param {Request} $request
     * @return array
     */
    public function orderWhere($all, $worker_id)
    {
        $where = [];
        //查询这个产品的订单
        if (isset($all['contract_no'])) {
            $where[] = ["order.contract_no", 'like', '%' . $all['contract_no'] . '%'];
        }
        //查询这个产品的订单
        if (isset($all['product_id'])) {
            $where[] = ["order.product_id", $all['product_id']];
        }
        //查询订单时间,开始时间
        if (isset($all['start_time'])) {
            $where[] = ["order.valid_time", ">=", $all['start_time']];
        }
        //查询订单时间，结束时间
        if (isset($all['end_time'])) {
            $where[] = ["order.valid_time", "<=", $all['end_time']];
        }
        //查询这个人的订单
        if (isset($all['user_id'])) {
            $where[] = ["order.user_id", $all['user_id']];
        }
        //查看是初审还是复审
        if (isset($all['audit_step'])) {
            $where[] = ["order.audit_step", $all['audit_step']];
        }
        //查询订单状态  -1是草稿 1是待付款  2是审核中   3是待生效  4是已生效 5是拒绝  6是已结束
        if (isset($all['order_status'])) {
            $where[] = ["order.order_status", $all['order_status']];
        } else {
            $where[] = ["order.order_status", "!=", -1]; // 不能是草稿
            $where[] = ["order.order_status", "!=", 5]; // 不能是已拒绝订单
        }

        return $where;
    }
}
